package com.miao.hong.fang.util;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.MailSender;

import com.miao.hong.fang.admin.service.AdminService;
import com.miao.hong.fang.course.service.CourseService;
import com.miao.hong.fang.index.service.IndexService;
import com.miao.hong.fang.teacher.service.TeacherService;
import com.miao.hong.fang.user.service.UserService;

public class SpringBeansHolder {

	private static SpringBeansHolder springBeansHolder;
	
	public static SpringBeansHolder getInstance() {
		
		if(springBeansHolder == null) {
			springBeansHolder = new SpringBeansHolder();
		}
		return springBeansHolder;
	}
	
	private IndexService indexService;
	private AdminService adminService;
	private CourseService courseService;
	private TeacherService teacherService;
	private UserService userService;
	private JdbcTemplate jdbcTemplate;
	private MailSender mailSender;
	
	
	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public IndexService getIndexService() {
		return indexService;
	}

	public void setIndexService(IndexService indexService) {
		this.indexService = indexService;
	}
	
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public MailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void injectBeans(DefaultListableBeanFactory factory) {
		
		IndexService indexService = (IndexService) factory.getBean("indexService");
		getInstance().setIndexService(indexService);	
		
		AdminService adminService = (AdminService) factory.getBean("adminService");
		getInstance().setAdminService(adminService);
		
		CourseService courseService = (CourseService) factory.getBean("courseService");
		getInstance().setCourseService(courseService);
		
		TeacherService teacherService = (TeacherService) factory.getBean("teacherService");
		getInstance().setTeacherService(teacherService);
		
		JdbcTemplate jdbcTemplate = (JdbcTemplate) factory.getBean("jdbcTemplate");
		getInstance().setJdbcTemplate(jdbcTemplate);
		
		UserService userService = (UserService) factory.getBean("userService");
		getInstance().setUserService(userService);
		
		MailSender mailSender = (MailSender) factory.getBean("mailSender");
		getInstance().setMailSender(mailSender);
	}
	
}
